package Dao;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

import Persistencia.HibernateUtil;

public class ComparativaPreciosDao {
	
	private static ComparativaPreciosDao instancia = null;
	private static SessionFactory sf = null;
	
	public static ComparativaPreciosDao getInstancia() {
		if(instancia == null){
		sf = HibernateUtil.getSessionFactory();
			instancia = new ComparativaPreciosDao();
		} 
		return instancia;
	}

	
	public List buscarComparativaPrecios() {
		
		List lista = new ArrayList();
		
			Session session = sf.openSession();
			session.beginTransaction();
			Query query = session.createQuery("from Proveedor");
			lista = query.list();			
			session.flush();
			session.getTransaction().commit();
			session.close();
			
			return lista;
		
	}


	public List buscarPrecios(String codigo, String marca, String origen) {
		List lista = new ArrayList();
		
		Session session = sf.openSession();
		session.beginTransaction();
		
		SQLQuery query = session.createSQLQuery(
				"select r.precio, r.codigo, r.marca, r.caracteristica, o.descripcion " +
				"from rodamiento r, pais_origen o " +
				"where r.id_origen = o.pais_origen_id " +
				"and (? is null or r.codigo = ?) " +
				"and (? is null or r.marca = ?) " +
				"and (? is null or r.id_origen = ?)");				
		query.setString(0, codigo);
		query.setString(1, codigo);
		query.setString(2, marca);
		query.setString(3, marca);
		query.setString(4, origen);
		query.setString(5, origen);
		lista =  query.list();		
		session.flush();
		session.getTransaction().commit();
		session.close();
		
		return lista;		
	}

}
